package www.hz.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import www.hz.domain.strategy.model.entity.RuleActionEntity;
import www.hz.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import www.hz.domain.strategy.repository.IStrategyRepository;
import www.hz.domain.strategy.service.rule.chain.AbstractLogicChain;
import www.hz.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import www.hz.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import www.hz.types.common.Constants;

import javax.annotation.Resource;

/**
 * @className: BackListLogicChain
 * @Description: TODO
 * @version: v1.８.0
 * @author: hz
 * @date: 2025/3/16 9:36
 */
@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository repository;
    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        log.info("抽奖责任链-黑名单开始 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        //查询规则值（100:user001,user002,user003）
        String ruleValue=repository.queryStrategyRuleValue(strategyId,ruleModel());
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        Integer awardId=Integer.parseInt(splitRuleValue[0]);
        //过滤其他规则
        String[] blackUserIds=splitRuleValue[1].split(Constants.SPLIT);
        for(String blackUserId:blackUserIds){
            if(userId.equals(blackUserId)) {
                return DefaultChainFactory.StrategyAwardVO.builder().awardId(awardId).logicModel(DefaultChainFactory.LogicModel.RULE_BLACKLIST.getCode()).build();
            }
        }
        // 过滤其他责任链
        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        return next().logic(userId, strategyId);
    }

    @Override
    public String ruleModel() {
        return "rule_blacklist";
    }
}